<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">变更单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=contract_bian','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=contract_bian','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>

                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>合同名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="caiid" class="layui-input caiid" onclick="choose_contract_cai()">
                            <div class="choose_div" id="caiid" key="1"></div>
                            <span class="choose_one" onclick="choose_contract_cai()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">原合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="yuanfee" placeholder="请输入" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>本次变更金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="bianfee" id="bianfee" placeholder="请输入" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>变更后合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="houfee" id="houfee" placeholder="请输入" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>变更日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>经办人</label>
                        <div class="layui-input-block">
                            <input type="text" name="baner" id="baner" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联单据</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>

            <?php include '../public/upinfo.php'?>
        </div>


        <div class="block-generic" id="caiDiv" budgetid="0">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>变更明细</span></div>
                        <div>
                            <button type="button" id="list_budget" onclick="choose_engin_budget_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn layui-hide">从预算明细新增</button>
                            <button type="button" onclick="choose_contract_cai_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从合同明细变更</button>
                            <button type="button" id="list_add" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row layui-hide">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th class="layui-hide"><text>*</text>成本科目</th>
                            <th class="layui-hide"><text>*</text>预算项</th>
                            <th><text>*</text>名称及规格</th>
                            <th><text>*</text>单位</th>
                            <th>原合同数量</th>
                            <th>原合同单价(元)</th>
                            <th>原合同金额(元)</th>
                            <th>调整数量</th>
                            <th>调整单价(元)</th>
                            <th>变更后数量</th>
                            <th>变更后单价(元)</th>
                            <th>变更后金额(元)</th>
                            <th>变更原因</th>
                            <th>来源</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var parent_caiid = getQueryVariable('caiid') || 0;

    $("#commentForm").validate({
        rules: {
            dt: {required: true},
            baner: {required: true},
        },
        messages:{
            dt: {required: "请选择"},
            baner: {required: "请输入"},
        }
    });


    function choose_contract_cai(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目");return false;
        }
        openPage('../public/choose_contract_cai.php?dom=caiid&page=contract_bian&enginid='+enginid,'80%','80%')
    }


    function choose_engin_budget_list(obj){
        var budgetid = $("#caiDiv").attr('budgetid');
        openPage('../public/choose_engin_budget_list.php?page=contract_bian&budgetid='+budgetid,'80%','80%');
    }
    function choose_engin_budget_list_one(obj){
        var domkey = $(obj).parents('tr').index();
        var budgetid = $("#caiDiv").attr('budgetid');
        openPage('../public/choose_engin_budget_list.php?choose_type=radio&page=contract_bian_one&budgetid='+budgetid+'&domkey='+domkey,'80%','80%');
    }


    function choose_contract_cai_list(obj){
        var caiid = get_choose_val('caiid');
        if(!caiid){
            facade.error("请先选择合同名称");return false;
        }
        openPage('../public/choose_contract_cai_list.php?page=contract_bian&caiid='+caiid,'80%','80%');
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_add_contract_bian, data:{caiid: parent_caiid}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                }

                var caiinfo = res.data.caiinfo;
                if(caiinfo){
                    set_choose_btn('caiid',caiinfo.id,caiinfo.name);
                    $("#yuanfee").val(caiinfo.money);
                }

                var engininfo = res.data.engininfo;
                if(engininfo){

                    $("#caiDiv").attr('budgetid', engininfo.budgetid);
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }

                $("#number").val(data.number);
                $("#dt").val(data.dt);
                $("#baner").val(data.baner);


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td class="layui-hide"><input type="text" subid="0" class="layui-input subname disabled"></td>';
                listAddStr += '<td class="layui-hide"><input type="text" disabled class="layui-input guiname disabled"></td>';
                listAddStr += '<td><input type="text" class="layui-input name" listid="0"></td>';
                listAddStr += '<td><input type="text" class="layui-input unit"></td>';
                listAddStr += '<td><input type="text" class="layui-input num_yuan disabled" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input price_yuan disabled" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input money_yuan disabled" readonly></td>';
                listAddStr += '<td><input type="number" class="layui-input num_tiao"></td>';
                listAddStr += '<td><input type="number" class="layui-input price_tiao"></td>';
                listAddStr += '<td><input type="number" class="layui-input num disabled" readonly></td>';
                listAddStr += '<td><input type="number" class="layui-input price disabled" readonly></td>';
                listAddStr += '<td><input type="number" class="layui-input money disabled" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input lai disabled" value="新增"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);
                update_refresh_row_func();


                public_upload_file_load('upload_files');
                render_from();
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.enginid = get_choose_val('enginid');
            data.field.caiid = get_choose_val('caiid');

            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                formValid('caiid');
                if(data.field.caiid == undefined || !data.field.caiid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var subid = $(this).find('.subname').attr('subid');
                    var listid = $(this).find('.name').attr('listid');
                    var name = $(this).find('.name').val();
                    var unit = $(this).find('.unit').val();
                    var num_yuan = $(this).find('.num_yuan').val();
                    var price_yuan = $(this).find('.price_yuan').val();
                    var money_yuan = $(this).find('.money_yuan').val();
                    var num_tiao = $(this).find('.num_tiao').val();
                    var price_tiao = $(this).find('.price_tiao').val();
                    var num = $(this).find('.num').val();
                    var price = $(this).find('.price').val();
                    var money = $(this).find('.money').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;

                    if(data.field.isturn > 0){
                        if(!name){
                            throw new Error('第'+index+'行，请输入名称');
                        }
                        if(!unit){
                            throw new Error('第'+index+'行，请输入单位');
                        }
                        if(num_tiao == ''){
                            throw new Error('第'+index+'行，请输入调整数量');
                        }
                        if(price_tiao == ''){
                            throw new Error('第'+index+'行，请输入调整单价');
                        }

                        if(listid > 0){
                            var chongKey = check_chongfu(i, listid);
                            if(chongKey){
                                throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                            }
                        }
                    }


                    list.push({
                        subid:subid,
                        listid:listid,
                        name:name,
                        unit:unit,
                        num_yuan:num_yuan,
                        price_yuan:price_yuan,
                        money_yuan:money_yuan,
                        num_tiao:num_tiao,
                        price_tiao:price_tiao,
                        num:num,
                        price:price,
                        money:money,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_contract_bian, ask_flowdata_edit_contract_bian, $(this).attr('but'));
            return false;
        });

    });


    //检测重复
    function check_chongfu(index, listid){
        try {
            $('#list_list tr').each(function (i) {
                var cur_listid = $(this).find('.name').attr('listid');
                if(index != i){
                    if(cur_listid == listid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }


    function choose_enginid_delete() {
        $("#caiid").html('');
        $("#list_list").html('');
    }

    function choose_caiid_delete() {
        $("#list_list").html('');
    }

    $('#list_list').on('input propertychange','.num_tiao,.price_tiao',function () {
        jisuan_money_all();
    });
    function jisuan_money_all() {
        var allmoney = 0;
        $('#list_list tr').each(function (i) {
            var num_yuan = ($(this).find('.num_yuan').val()) || 0;
            var price_yuan = ($(this).find('.price_yuan').val()) || 0;
            var num_tiao = ($(this).find('.num_tiao').val()) || 0;
            var price_tiao = ($(this).find('.price_tiao').val()) || 0;

            var num = parseFloat(num_yuan) + parseFloat(num_tiao);
            var price = parseFloat(price_yuan) + parseFloat(price_tiao);
            var money = (num * price).toFixed(2);


            var money_yuan = $(this).find('.money_yuan').val() || 0;
            allmoney = parseFloat(allmoney) + (parseFloat(money) - parseFloat(money_yuan));



            $(this).find('.num').val(parseValidNum(num));
            $(this).find('.price').val(parseValidNum(price));
            $(this).find('.money').val(parseValidNum(money));
        });
        $("#bianfee").val(allmoney);

        var yuanfee = ($("#yuanfee").val()) || 0;

        var houfee = parseFloat(yuanfee) + parseFloat(allmoney);
        $("#houfee").val(houfee);
        initParseMoney();
    }




    function set_list_layout() {
        var budgetid = $("#caiDiv").attr('budgetid');
        if(budgetid > 0){
            $("#caiDiv table tr th, #caiDiv table tr td").removeClass('layui-hide');
            $("#list_budget").removeClass('layui-hide');
            $("#list_add").addClass('layui-hide');
        }else{
            $("#caiDiv table tr th:nth-child(2)").addClass('layui-hide');
            $("#caiDiv table tr th:nth-child(3)").addClass('layui-hide');
            $("#caiDiv table tr td:nth-child(2)").addClass('layui-hide');
            $("#caiDiv table tr td:nth-child(3)").addClass('layui-hide');
            $("#list_budget").addClass('layui-hide');
            $("#list_add").removeClass('layui-hide');
        }
    }

    function update_refresh_row_func() {
        render_date();
        render_from();
        set_list_layout();
        jisuan_money_all();

        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }

</script>
</body>
</html>
